﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Web.UI.WebControls;
using System.Web;
using Microsoft.SharePoint;
using System.Diagnostics;

namespace LanguageConfigurationSolution
{
    public class LanguageParameter : Parameter
    {
        private string key;
        private string _ConfigurationlistName;
        public string Key
        {
            get { return key; }
            set { key = value; }
        }

        public string ConfigurationlistName
        {
            get { return _ConfigurationlistName; }
            set { _ConfigurationlistName = value; }
        }


        protected override object Evaluate(HttpContext context, System.Web.UI.Control control)
        {
            if (!string.IsNullOrEmpty(ConfigurationlistName) && !string.IsNullOrEmpty(Key))
            {
                return GetConfigValue();
            }
            else
            {
                return base.Evaluate(context, control);
            }
        }

        private string GetConfigValue()
        {

            string returnValue = string.Empty;
            try
            {
                returnValue = ConfigurationReader.GetConfigurationValue(Key, _ConfigurationlistName, "");

            }
            catch (Exception ex)
            {
                SPSecurity.RunWithElevatedPrivileges(delegate
                {
                    EventLog.WriteEntry( 
                        this.GetType().FullName, 
                        string.Format("Failed to retrieve the configuration value for key '{0}' from the list '{1}'.{2}{3}", new string[] {Key, _ConfigurationlistName, Environment.NewLine, ex.ToString() } ), 
                        EventLogEntryType.Error);
                } );
            }

            return returnValue;
        }
    }
}
